package com.example.gesture.PointerInput

import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectTransformGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.draw.scale
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import kotlin.math.roundToInt

//panZoomLock(可选)： 当拖动或缩放手势发生时是否支持旋转
//onGesture(必须)：当拖动、缩放或旋转手势发生时回调

@Composable
fun TransformGestureDemo() {
    val boxSize = 100.dp
    var offset by remember {
        mutableStateOf(Offset.Zero)
    }
    var ratationAngle by remember {
        mutableStateOf(0f)
    }
    var scale by remember {
        mutableStateOf(1f)
    }
    Box(
        modifier = Modifier
            .size(boxSize)
            .rotate(ratationAngle)
            .scale(scale)
            .offset { IntOffset(offset.x.roundToInt() , offset.y.roundToInt()) }
            .pointerInput(Unit) {
                detectTransformGestures(
                    panZoomLock = true,//平移或放大时是否可以旋转
                    onGesture = { centroid: Offset, pan: Offset, zoom: Float, rotation: Float ->
                        offset += pan
                        scale *= zoom
                        ratationAngle += rotation
                    }
                )
            }
            .background(Color.Green)
    )
}